<?php
namespace Home\Controller;
use Think\Controller;
use Home\Event\LetterEvent;
//set_time_limit(100);
/**
 * 短信
 * Class LetterController
 * @package Home\Controller
 */

class LetterController extends Controller {
    public $obj;
    public function __construct(){
        parent::__construct();
        //$this->obj = A('WeixinWeb','Event');
        $this->obj = new \Home\Event\LetterEvent();
    }

    /**
     *判断是否已经验证过手机
     * $type 默认是接口 为1的时候为函数
     */
    public function verify($type = 0){
        if($type){
            return $this->doVerify();
        }else{
            $this->ajaxReturn($this->doVerify());
        }
    }
    public function doVerify(){
        $t_userInfo = session('t_userInfo');
        if(!$t_userInfo){
            $userInfo = session('userInfo');
            if(!$userInfo){
                return false;
            }else{
                cookie('loginStatus',1);
                return true;
            }
        }else{
            //查询会员信息入session
            $userInfo =  M('b2c_members')->where(array('openid'=>$t_userInfo['openid']))->find();
            if(!$userInfo){
                return false;
            }
            if(!$userInfo['mobile']){
                return false;
            }
            if($userInfo['mobile'] != $t_userInfo['mobile']){
                $t_status = M('b2c_temp_user')->where(array('openid'=>$t_userInfo['openid']))->save(['mobile'=>$userInfo['mobile']]);
                if($t_status === false){
                    return false;
                }
            }
            session('userInfo',$userInfo);
            cookie('loginStatus',1);
            return true;
        }
    }

    /**
     * 验证手机(生成验证码)
     * @param $mobile 手机号
     * @param $time 有效时间
     */
    public function verifyMobileCode(){
        $mobile = I('mobile') or exit(json_encode(['stat' => false,'msg' => '验证失败！']));
        $date = C('SMS_DATE');
        if(!isMobile($mobile)){
            exit(json_encode(['stat' => false,'msg' => '手机号码不对，请核对！']));
        }
        $stat = $this->obj->verifyMobileCode($mobile,$date);
        exit(json_encode($stat));
    }

    /**
     * 验证验证码
     *  @param $mobile
     * @param $code
     */
    public function verifyMobile(){
        //$mobile = I('mobile') or exit(json_encode(['stat' => false,'msg' => '验证失败！']));
        $mobile = session('verifyMobile');
        $t_userInfo = session('t_userInfo');
        $ildMobile = $t_userInfo['mobile'];
        if($ildMobile && $mobile != $ildMobile){
            exit(json_encode(['stat' => false,'msg' => '验证失败！']));
        }

        $code = I('code') or exit(json_encode(['stat' => false,'msg' => '验证失败！']));
        //$type = I('type',0);//0-为验证，1-非0为登陆
        $stat = $this->obj->verifyMobile($code);
        if($stat['stat'] == false){
            exit(json_encode($stat));
        }
        //验证码通过存数据到menber表,如果是游客同时要更新游客表的手机号
        $model = M();
        //开启事物
        $model->startTrans();

        $members_data = [
            'member_lv_id' => 1,
            'status' => '1',
            'point' => 0,
            'mobile' => $mobile,
            'sex' => '2',
            'advance' => 0.00,
            'advance_freeze' => 0.00,
        ];
        if($t_userInfo){
            //判断手机号是否已被其他账号使用
            if(M('b2c_members')->where(['mobile' => $mobile,'openid' =>['neq',$t_userInfo['openid']] ])->find()){
                $this->ajaxReturn( ['status' => false,'msg' => '该手机号已被他人使用！']);
            }
            $members_data['default_local_id'] = $t_userInfo['default_local_id'] ? $t_userInfo['default_local_id'] : 0;
            $members_data['nickname'] = $t_userInfo['nickname'];
            $members_data['openid'] = $t_userInfo['openid'];
            //更新b2c_temp_user表
            $temp_id = $model->table("sdb_b2c_temp_user")->where(['t_u_id' => $t_userInfo['t_u_id']])->save(['mobile' => $mobile,'authentication' => '1']);
            //exit(json_encode($temp_id));
        }else{
            $members_data['openid'] = session('wx_openid');
            //该手机号存在则已认证，写入session
            if($userInfo = M('b2c_members')->where(['mobile' => $mobile])->find()){
                session('userInfo',$userInfo);
                $this->ajaxReturn($stat);
            }
            $temp_id = true;
        }
        //查询该电话号码是否已认证
        $userInfo = $model->table("sdb_b2c_members")->where(['mobile' => $mobile])->find();
        $members_id = true;
        if(!$userInfo){
            $members_data['reg_ip'] = get_client_ip();
            $members_data['regtime'] = time();
            //$members_id = $model->table("sdb_b2c_members")->add($members_data);
            $m_data =  M('b2c_members')->where(['openid'=>$t_userInfo['openid']])->find();
            if(empty($m_data)){
                $members_data['mobile'] = $mobile;
                $members_id = M('b2c_members')->add($members_data);
            }else{
                $members_id = $model->table("sdb_b2c_members")->where(['openid'=>$t_userInfo['openid']])->save($members_data);
            }
            $userInfo = $model->table("sdb_b2c_members")->where(['mobile' => $mobile])->find();
        }
        session('userInfo',$userInfo);
        if ($members_id !== false && $temp_id !== false){
            // 提交事务
            $model->commit();
            exit(json_encode($stat));
        }else{
            // 事务回滚
            $model->rollback();
            exit(json_encode(['stat' => false,'msg' => '验证失败333！']));
            //$this->ajaxReturn(array('stat' => false,'msg' => '验证失败！'));

        }

    }

    /**
     * 验证手机号是否正确
     * @param INT $mobile
     */
    /*public function isMobile($mobile) {
        if (!is_numeric($mobile)) {
            return false;
        }
        return preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $mobile) ? true : false;
    }*/

   public function __destruct(){
       parent::__destruct();
       unset($this->obj);
   }


    

   

}